
//获取应用实例
var api = require("../../api/api.js");
var apiRequest = require("../../api/wxRequest.js");
var storageManage = require("../../manage/storageManage.js");
var utils = require("../../utils/utils.js");
var itemList = require("../../template/commodity-item-list/commodity-item-list.js");

Page({
  data: {
    banners: [],
    goodsList: [],
    shortcuts: [],
    isScroll: false
  },
  onReady: function () {
    //获得modalContainer组件
    this.modalContainer = this.selectComponent("#modalContainer");
  },
  onLoad: function (options) {
    var that = this;
    wx.showLoading()
    itemList.init.apply(this, []);//引入方法
    api.default.check(function () {
      //判断是否注册过
      console.log("已经调取服务器登录")
      if (storageManage.getServiceUser().login) {

        api.default.api('get_conf', {}, function (resD) {
          storageManage.setGlobalConfig(resD.data);

          var user_state = resD.data.user_state;
          var user_affirm = resD.data.user_affirm;

          if (user_state == 0 || user_state == 1 || (user_state == 2 && user_affirm == 0)) { //被驳回 和 正在审核 通过没点确认
            wx.reLaunch({
              url: '../loginResult/loginResult'
            })
            return;
          }

          api.default.api('get_home', {}, that.successCallback, that.errorCallback);



        }, function () {

        })

      } else {
        wx.reLaunch({
          url: '../cover/cover',
        })
      }
    })
  },
  //get_home
  successCallback: function (data) {
    api.default.api('home_news', {}, this.newsSuccessCallback, this.newsErrorCallback);
    this.setData({
      "banners": data.data.banners,
      "goodsList": data.data.commodities,
      // "shortcuts": data.data.shortcuts,
      "isHidden": true
    });
    wx.hideLoading()
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  errorCallback: function (e, ed) {
    console.info("get_home " + e)
    wx.hideLoading()
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  //home_news
  newsSuccessCallback: function (data) {
    this.setData({
      head_lines: data.data
    });
    wx.hideLoading()
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  newsErrorCallback: function (e, ed) {
    console.info("home_news " + e)
    wx.hideLoading()
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  //详情跳转
  onCommodityClick: utils.throttle(function (e) {
    api.default.send_template(e);
    wx.navigateTo({
      url: '../detail/detail?commodity_id=' + e.currentTarget.id,
    });
  }),
  //组件传递的方法
  hideModal: function () {
    console.log("组件被关闭")
    this.setData({
      isScroll: false
    })
  },
  //资讯的跳转
  newsTap: utils.throttle(function (e) {
    console.log("id=" + e.currentTarget.id);
    wx.navigateTo({
      url: '../news/news?news_id=' + e.currentTarget.id,
    });
  }, 500),

  //跳转
  funTap: utils.throttle(function (e) {
    var btype = e.currentTarget.dataset.type;
    var param = e.currentTarget.dataset.value;
    switch (btype) {
      case 'commodity': //商品详情
        wx.navigateTo({
          url: '../detail/detail?commodity_id=' + param["commodity_id"],
        });
        break;
      case 'category': //分类
        wx.navigateTo({
          url: '../category/category?id=' + param["category_id"],
        });
        break;
      case 'about_us': //关于我们

        wx.navigateTo({
          url: '../contactUs/contactUs',
        });
        break;
      case 'favourite': //收藏

        wx.navigateTo({
          url: '../favor/favor',
        });
        break;
      case 'order': //订单

        wx.navigateTo({
          url: '../order/order',
        });
        break;
    }
  }, 500),

  //下拉刷新
  onPullDownRefresh: function () {
    this.data.banners = [];
    this.data.goodsList = [];
    api.default.api('get_home', {}, this.successCallback, this.errorCallback);
    api.default.api('home_news', {}, this.newsSuccessCallback, this.newsErrorCallback);

  },
  //上拉加载
  onReachBottom: function () {

  },

  /**
  * 生命周期函数--监听页面隐藏
  */
  onHide: function () {

  },
  //分享
  onShareAppMessage: function () {
    return {
      title: "首页",
      path: "/pages/index/index",
      success: function (res) {
        console.log("转发成功");
      },
      fail: function (res) {
        // 转发失败
        console.log("转发失败");
      }

    }

  }


})
